﻿#region

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using VinhSon.Intranet.Bussiness;
using VinhSon.Intranet.Models;

#endregion

namespace VinhSon.Intranet.Controllers
{
    public class PermissionController : BaseController
    {
        //
        // GET: /Permission/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult GetList(int start,
                                    int limit,
                                    string sort)
        {
            var query = UnitOfWork.PermissionRepository.Select(c => c.Inactive == false)
                                  .Select(c => new
                                                   {
                                                       c.Id,
                                                       c.Name,
                                                       c.GroupName,
                                                       c.Description,
                                                       c.KeyName
                                                   });
            return Content(FormatResult(query,
                                        c => c.OrderBy(v => v.GroupName),
                                        start,
                                        limit,
                                        sort,
                                        PermissionModel.Sort),
                           "json");
        }

        public ActionResult Add()
        {
            return View();
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Add(PermissionModel permissionModel)
        {
            if (ModelState.IsValid)
            {
                UpdateResult result = new UpdateResult();
                Permission permission = permissionModel;
                if (UnitOfWork.PermissionRepository.CheckInsertPermission(permission))
                {
                    UnitOfWork.PermissionRepository.Insert(permission);
                    UnitOfWork.Save();

                    AppUtil.ResetAuthenticationService = true;
                }
                return Json(Server.FormatResultToJson(result));
            }
            return Json(Server.FormatResultToJson(ModelState));
        }

        public ActionResult Edit(string id)
        {
            int guid;
            if (int.TryParse(id,
                            out guid))
            {
                Permission permission = UnitOfWork.PermissionRepository.SelectbyId(guid);
                if (permission != null)
                {
                    PermissionModel permissionModel = permission;
                    return View(permissionModel);
                }
            }
            return RedirectToAction("Index");
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(PermissionModel permissionModel)
        {
            if (ModelState.IsValid)
            {
                UpdateResult result = new UpdateResult();
                Permission permission = UnitOfWork.PermissionRepository.SelectbyId(permissionModel.Id);
                permissionModel.Update(permission);
                if (UnitOfWork.PermissionRepository.CheckUpdatePermission(permission))
                {
                    UnitOfWork.PermissionRepository.Update(permission);
                    UnitOfWork.Save();

                    AppUtil.ResetAuthenticationService = true;

                    return Json(Server.FormatResultToJson(result));
                }
            }
            return Json(Server.FormatResultToJson(ModelState));
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            UpdateResult result = new UpdateResult();
            if (id != null)
            {
                string[] s = id.Split(',');
                int guid;
                List<int> list = (from s1 in s
                                  where int.TryParse(s1,
                                                     out guid)
                                  select int.Parse(s1)).ToList();

                if (UnitOfWork.PermissionRepository.CheckDeletePermission(list))
                {
                    UnitOfWork.PermissionRepository.Delete(list);
                    UnitOfWork.Save();

                    AppUtil.ResetAuthenticationService = true;
                }
                else
                {
                    result.State = 4;
                    result.KeyLanguage = UpdateResult.INFORMATION_IN_USED;
                }
            }
            else
            {
                result.State = 4;
                result.KeyLanguage = UpdateResult.INFORMATION_IN_USED;
            }
            return Json(Server.FormatResultToJson(result));
        }
    }
}
